package 力扣._141_环形链表;


public class Solution {
    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }


    //快慢指针
    public boolean hasCycle(ListNode head) {
        if (head == null)
            return false;

        ListNode slow = head;
        ListNode fast = head.next;
        while (slow != null && fast != null) {
            if (slow == fast)
                return true;
            slow = slow.next;
            if (fast.next == null)
                return false;
            else
                fast = fast.next.next;
        }
        return false;
    }

    public static void main(String[] args) {

    }
}